<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="../../../attr.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!doctype html>
<html>
<meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="${ctx}/css/sapar.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/css/common.css" />
<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/sapar.js"></script>
<script type="text/javascript" src="${ctx}/js/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/assets/layer/layer.js"></script>
<script type="text/javascript" src="${ctx}/js/kkpager.min.js"></script>
<script type="text/javascript" src="${ctx}/js/mask.js"></script>
<link rel="stylesheet" type="text/css" href="${ctx}/css/kkpager_blue.css" />
<script type="text/javascript" src="${ctx}/js/jquery-1.10.1.js"></script>
<title>厂家包发放</title>
<style>
.kv-item select {
    width: 150px;
    height: 24px;
    border: solid gray 1px;
    margin-top: 4px;
}
#shou {
	margin-left: 8px;
}
#fafangrentiaoma{
    margin-top: 5px;
}
#yici {
	margin-left: 8px;
}
#yifa{
color:green;
font-weight:bold;
}
#weifa{
color:red;
font-weight:bold;
}
#mm{
    margin-top: 2px;
}
th{
text-align: center;
}
td{
text-align: center;
}
</style>
</head>

<body>
<div id="mask" class="mask"></div>
    <audio src="../audio/Beep_Short.mp3" id="audio"></audio><!-- 成功提示音 -->
    <audio src="../audio/Alarm_Clock (1).mp3" id="audio1"></audio><!-- 失败提示音 --> 
	<div id="saper-container">
		<div id="saper-hd"></div>
		<div id="saper-bd">
			<div class="subfiled clearfix">
				<h2>厂家包发放</h2>
			</div>
			<div class="subfiled-content">
				<div class="search-box clearfix">
					<div class="kv-item clearfix">
					    <label id="mm">包条码:</label>
						<div class="kv-item-content">
							<input type="text" name="fafangren" placeholder="包条码"
								 class="search_text" id="baotiaoma" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')">
						</div>
						<label id="mm">发放人条码:</label>
						<div class="kv-item-content">
							<input type="text" name="fafangren" placeholder="发放人条码"
								 class="search_text" id="fafangrentiaoma" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')">
						</div>
					</div>
                    <div class="kv-item clearfix">
                    <label id="mm">发放科室:</label>
						<div class="kv-item-content" id="xialakuang">
                            	<c:if test="${alldeparts != null}">
                            	<select id="keshi">
	                                <c:forEach items="${alldeparts}" var="pro">
		                            	<c:if test="${pro.departmentId == 15 }">
		                            	<option value="${pro.departmentId}">${pro.departmentName }</option>
		                                </c:if>
		                            </c:forEach>
		                            <option value="">厂家</option>                                                             
		                        </select>
                                </c:if>
                                
						</div>
					</div>
				    <a href="javascript:;" class="sapar-btn sapar-btn-recom query-btn" id="fafang">发放</a>
				</div>

				<!--表格开始-->
				<div class="table">
					<!--表格操作-->

					<div class="table-box">
						<table>
							<thead>
								<tr>
								  <th><input type="checkbox" id="checkall" name="checkall" /></th>
								  <th class="text-left">包条码</th>
			                      <th class="text-left">包名称</th>
			                      <th class="text-left">包类型</th>
			                      <th class="text-left">合格时间</th>
			                      <th class="text-left">发放状态</th> 
								</tr>
							</thead>
							<tbody>
							    <c:if test="${shoushubaoList == null || fn:length(shoushubaoList)==0 }">
                                    <tr class="no-data product"><td colspan="10" style="color: red">暂时没有数据</td></tr>
                                </c:if>
								<c:if test="${shoushubaoList != null && shoushubaoList != ''}">
									<c:forEach items="${shoushubaoList}" var="shoushubao">
										<c:if test="${shoushubao.tbPackageMuban.packageDepartid == null }">
											<tr>
											  <td><input  type="checkbox" name="key" value="${shoushubao.packageTiaoma}" data2="${shoushubao.fafangLiushui}"/></td>
											  <td>${shoushubao.packageTiaoma}</td>
						                      <td>${shoushubao.packageName}</td>
						                      <td>
						                        <c:if test="${shoushubao.tbPackageMuban.packageCategory ==0}">单封包</c:if>
						                        <c:if test="${shoushubao.tbPackageMuban.packageCategory ==1}">通用包</c:if>
						                        <c:if test="${shoushubao.tbPackageMuban.packageCategory == null}">厂家器械包</c:if>
						                      </td>
						                      <td><fmt:formatDate value="${shoushubao.miejunShenheTime}" type="date" pattern="yyyy-MM-dd HH:mm:ss"/></td>
						                      <td>
						                        <c:if test="${shoushubao.fafangLiushui==null }"><span id="weifa">未发</span></c:if>
						            			<c:if test="${shoushubao.packageLiuchengZhuangtai==10 && shoushubao.peibaoHege==1 && shoushubao.peibaoGuanbaoTime>=shoushubao.miejunShenheTime }"><span id="yifa">手术室(可发放给厂家)</span></c:if>
						            			<%-- <c:if test="${shoushubao.packageLiuchengZhuangtai>5 && shoushubao.packageLiuchengZhuangtai<=10 && shoushubao.peibaoHege==1 && shoushubao.peibaoGuanbaoTime<=shoushubao.miejunShenheTime}"><span id="yifa">手术室(未进行二次配包审核)</span></c:if> --%>
						            			<c:if test="${shoushubao.fafangLiushui!=null && shoushubao.peibaoGuanbaoTime<=shoushubao.miejunShenheTime }"><span id="yifa">手术室(未进行二次配包审核)</span></c:if>
						            			<%-- <c:if test="${shoushubao.packageLiuchengZhuangtai==10 && shoushubao.peibaoHege==0}"><span id="yifa">手术室(未进行二次配包审核)</span></c:if> --%>
						                      	<c:if test="${shoushubao.packageLiuchengZhuangtai>10}"><span id="yifa">厂家 </span></c:if>
						                      </td>
						                    </tr>
					                    </c:if>
									</c:forEach>
								</c:if>
							</tbody>
						</table>
					</div>
				</div>

				<!--表格结束-->
			</div>
		</div>
		<!--分页用到的 -->
		<div id="saper-ft">
			<div id="kkpager"></div>
		</div>
	</div>
</body>
<!-- -->
<script type="text/javascript">
window.onload = function(){
	$("#baotiaoma").focus();
}
	$(function(){
		$(document).keydown(function(event){
			var txt = $("#baotiaoma").val();
			var txt1 = $("#fafangrentiaoma").val();;
			var oPlay0 = document.getElementById("audio");
			var oPlay1 = document.getElementById("audio1");
			var baoFocus=$("#baotiaoma").is(":focus");
	        var renFocus=$("#fafangrentiaoma").is(":focus");
			var i = 1;
			if(event.keyCode==13){
				if(!isNullOrEmpty(txt)&&i==1){
					if(true==baoFocus){
						var flag=false;
						if(txt == ('stop')){
							//结束的提示音
							$("#fafangrentiaoma").focus();
						}else if(txt1 != null){
							$('input[name="key"]').each(function(){
								if($(this).val()==txt){
									flag=true;
			    		    		$(this).prop('checked',true);
			    		    	}
						    });
							txt = null;
						}
						if(flag){
							oPlay0.play();
						}else{
							oPlay1.play();
						}
					    $("#baotiaoma").val('');
					}
				}
				if(!isNullOrEmpty(txt1)&&i==1){
					if(renFocus==true){
					   if(!isNullOrEmpty(txt1)){
						  $.ajax({
							 type: "POST",
				             url:"${ctx}/employee/findByTioma.do?id="+txt1,
				             error: function(request) {
				            	 	layer.alert("网络故障，请稍后再试!");
				             },
				             success: function(data) {
				            	 if(data.statue=="true"){
				            		 oPlay0.play();
				            		 $("#fafang").click();
				            	 }else{
				            		 oPlay1.play();
				            		 $("#fafangrentiaoma").val('');
				            		 $("#fafangrentiaoma").focus();
				            	 }
					         }
					      });
					   }
					}
				}
			}
		});
		/* var totalPage = ${total};
		var pageNo = getParameter('pageIndex');
		if(!pageNo){
			pageNo = 1;
		}
		//生成分页
		//有些参数是可选的，比如lang，若不传有默认值
		kkpager.generPageHtml({
			pno : pageNo,
			//总页码
			total : totalPage,
			//链接前部
			hrefFormer : '${ctx}/wupingfafang/shoushushibaoList',
			//链接尾部
			hrefLatter : '.do',
			//这些参数传过去干嘛的
			getLink : function(n){
				return this.hrefFormer + this.hrefLatter + "?pageIndex="+n+"&searchTxt="+$(".search_text").val();
			}
		}); */
		
		$("#checkall").click(function(){
   		    if(this.checked){
   		        $("input[name='key']").prop('checked', true)
   		    }else{
   		        $("input[name='key']").prop('checked', false)
   		    }
		});
	  
		$("#fafang").click(function(){
			
			var btn = true;
			var tiaomas=getSelRecord();//复选框选中的包
			var ffrtiaoma=$("#fafangrentiaoma").val();
//			var fafangzhuangtai=getfafangzhuangtai();
			var keshi=$("#keshi").val();
			if(isNullOrEmpty(ffrtiaoma)){
				alert("请输入发放人条码");
				btn=false;
				return btn;
			}
			/* if(fafangzhuangtai.length>0){
				alert("请不要选择已经发放过的包!");
				return false;
			} */
			if(tiaomas.length==0){
				alert("选择要发放的包");
				btn = false;
				return btn;
			}
 			if(btn){
		    	showMask();
		    	$.ajax({
		            type: "POST",
		            url:"${ctx}/wupingfafang/changjiabaofafang.do?baotiaomas="+tiaomas.join()+"&fafangren="+ffrtiaoma+"&keshi="+keshi,
		            error: function(request) {
		           	 	layer.alert("网络故障，请稍后再试!");
		            },
		            success: function(data) {
		            	$("#mask").hide();
			           	if(data.status=="true"){
			 	           	layer.alert("发放成功", function(){
			 	           	$("#tiaomaluru").val("");
			 	           		window.location.reload();
			 	           		if($('input[name="key"]:checked')){
			 	           		      $('input[name="key"]').prop("checked", false);
			 	           		}
		          		    });
			           	}else{
			           		layer.msg(data.msg);
			           	}
		            }
		        });
		    }
		});
	  
	    $("#saomafafang").click(function(){
	    	
	    });

	});
	
	//接受条码枪的录入
	function callbackPackData(data){
		var baoArr = data;//扫码传入
		var baoTmArr = [];//页面显示
	    var wybao=[];
		var btnArr=[];
	    $('input[name="key"]').each(function(){
	    	baoTmArr.push($(this).val());
		});
	    var bao=null;
	    var baoTm=null;
	    for(var i=0;i<baoArr.length;i++){
	    	btnArr=[];
	    	bao=baoArr[i];
	    	for(var j=0;j<baoTmArr.length;j++){
	    		baoTm=baoTmArr[j];
	    		if(bao==baoTm){
	    			$('input[name="key"]').each(function(){
	    		    	if($(this).val()==bao){
	    		    		$(this).prop('checked',true);
	    		    	}
	    			});
	    			
	    		}else{
	   	    		btnArr.push(baoTm);
	    		}
	    	}
	    	if(btnArr.length==baoTmArr.length){
				wybao.push(bao);
	    	}
	    }
	    if(wybao.length!=0){
			alert("不能进行操作的包:"+wybao);
		}
	}
	
	function getSelRecord(){
		var chk_value =[];
		$('input[name="key"]:checked').each(function(){
			chk_value.push($(this).val()); 
		});
		return chk_value;
	}  
	/* function getfafangzhuangtai(){
		var lishuis =[];
		$('input[name="key"]:checked').each(function(){
					if(!isNullOrEmpty($(this).attr("data2"))){
					lishuis.push($(this).attr("data2")); 
			}
		});
		return lishuis;
	} */  
	
  	function getParameter(name) { 
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
		var r = window.location.search.substr(1).match(reg); 
		if (r!=null) return unescape(r[2]); return null;
	}
  	
	
	
	//判断一个字符串是否为空
	function isNullOrEmpty(strVal){
		strVal = strVal.replace(/(^\s*)|(\s*$)/g,"");
		if(strVal == '' || strVal == null || strVal == undefined){
			return true;
		}else{
			return false;
		}
	}
	
	/* $(document).ready(function(){
		layer.open({
			  type: 2,
			  title: '发放扫码',
			  maxmin: true,
			  shadeClose: false, //点击遮罩关闭层
			  area : ['800px' , '520px'],
			  content: '${ctx}/page/wupinfafang/fafang_barcode.jsp'
		});
	}); */

</script>
</html>